import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
import javax.swing.JSlider;
public class GraphTS {
  private int i,j,k,y=26,t=0;
  private int MAX_VERTS;
  private int flagsort[];
  private int sorttop[];
  //======================================================================
  public void Init(ReadFileClass file,mxGraph graph,Object parent) throws InterruptedException{
    MAX_VERTS=file.GetMaxVertex();
    y=26;
    y=y*MAX_VERTS;
    sorttop = new int[MAX_VERTS];
    flagsort = new int[MAX_VERTS]; 
    for(i=0;i<MAX_VERTS;i++){
        flagsort[i] = 0;
        float theta = (float) (360/MAX_VERTS  * Math.PI * i / 180.0f);
        graph.insertVertex(parent, null,i+1,350+12*MAX_VERTS * Math.cos(theta),250+12*MAX_VERTS * Math.sin(theta), 24, 24,"shape=ellipse;");
        graph.setCellStyles(mxConstants.STYLE_FONTSIZE, "18",new Object[]{graph.getModel().getChildAt(parent,i)});
        graph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "white",new Object[]{graph.getModel().getChildAt(parent,i)});
    }
    for(i=0;i<MAX_VERTS;i++){
         for(j=0;j<MAX_VERTS;j++){
             if(file.matr[i][j]==1){   
                  graph.insertEdge(parent,null,null,graph.getModel().getChildAt(parent,i),graph.getModel().getChildAt(parent,j));
             }
         }
    }
  }
  //======================================================================
  public boolean TopSort(int i,ReadFileClass file,mxGraph graph,Object parent,mxGraph twograph,Object parent2,JSlider slider) throws InterruptedException{
    if(flagsort[i]!=0){
    if(flagsort[i]==1){
	graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "green", new Object[]{graph.getModel().getChildAt(parent,i)});
	Thread.sleep(slider.getValue());  
	return true;
    } else {
	return false;
    }
   } else {
        flagsort[i]=-1;
	graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "red", new Object[]{graph.getModel().getChildAt(parent,i)});
	Thread.sleep(slider.getValue()); 
	for(j=0;j<MAX_VERTS;j++){
            if(file.matr[i][j]!=0){
                graph.setCellStyles(mxConstants.STYLE_STROKECOLOR, "green",graph.getEdges(graph.getModel().getChildAt(parent,i),parent));
                if(!TopSort(j,file,graph,parent,twograph,parent2,slider)){
                    return false;
                }
            } else {}
	}
	sorttop[k]=i+1;
	flagsort[i]=1;
	graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "green", new Object[]{graph.getModel().getChildAt(parent,i)}); 
        twograph.insertVertex(parent2,null,i+1,80 ,y, 24, 24,"shape=ellipse;");
        twograph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "blue",new Object[]{twograph.getModel().getChildAt(parent2,t)});
        twograph.setCellStyles(mxConstants.STYLE_FONTCOLOR, "white",new Object[]{twograph.getModel().getChildAt(parent2,t)});
        twograph.setCellStyles(mxConstants.STYLE_FONTSIZE, "18",new Object[]{twograph.getModel().getChildAt(parent2,t)});
        t++;
        y-=26;
	Thread.sleep(slider.getValue()); 
	k++;j=0;
	return true;
	}
   }
  //======================================================================
   public void Sort(ReadFileClass file,mxGraph graph,Object parent,mxGraph twograph,Object parent2,JSlider slider) throws InterruptedException{         
    t=0;k=0;
    int v=1;
    for(i=0;i<MAX_VERTS;i++){
	if(!TopSort(i,file,graph,parent,twograph,parent2,slider)){
            ErrorWin f = new ErrorWin();
            f.createGUI("Есть цикл.");
            return;
	}
    }
    t-=1;
    for(i=k-1;i>=0;i--){
        Thread.sleep(slider.getValue());
        twograph.getModel().setValue(twograph.getModel().getChildAt(parent2,t), v);
        Thread.sleep(slider.getValue());
        twograph.getModel().remove(twograph.getModel().getChildAt(parent2,t));
        graph.getModel().setValue(graph.getModel().getChildAt(parent,sorttop[i]-1), v);
        graph.setCellStyles(mxConstants.STYLE_FILLCOLOR, "blue",new Object[]{graph.getModel().getChildAt(parent,sorttop[i]-1)});
	v++;
        t--;
    }
  }
   public int retmaxverts(){
       return MAX_VERTS;
   }
}